﻿@using Trauberman.SOChat.StarLeaderboard.Models
@using Trauberman.SOChat.StarLeaderboard.Domain
@model GroupedStarsViewModel
@{
    ViewBag.Title = "Star Leaderboard";
}

<table>
    <tr>
        <th>Place</th>
        <th>User</th>
        <th>Random Starred Message</th>
        <th>Total Stars</th>
        <th># of Starred Messages</th>
        <th>Average Stars Per Message</th>
    </tr>
    @{int row = 0;
      
      Random random = new Random();
    }
    @foreach (IGrouping<User, StarredMessage> userStars in Model.Stars.OrderByDescending(s => s.Sum(c => c.StarCount)))
    {
        <tr class='@(row++ % 2 == 0 ? "alternate" : null)'>
            <td>
                @row
            </td>
            <td>
                @Html.ActionLink(userStars.First().User.Name, "Index", "User", new { id = userStars.First().User.SOid }, null)
            </td>
            <td>
                @{var mostStarred = userStars.OrderBy(x => random.Next()).First();}
                <b>@mostStarred.StarCount</b> @Html.Raw(mostStarred.MessageText)
            </td>
            <td>
                @userStars.Sum(s => s.StarCount)
            </td>
            <td>
                @userStars.Count()
            </td>
            <td>
                @(((double)userStars.Sum(s => s.StarCount)/(double)userStars.Count()).ToString("n2"))
            </td>
        </tr>
    }
</table>
<script type="text/javascript" src="@Url.Content("~/Scripts/CustomScripts/Room.js")"></script>